﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
namespace IHISCETeim.DAL
{
    public class PartsBalanceDAL
    {
        public static int Add(List<PartsBalanceInfo> list)
        {
            int BId = 0;
            Database DB = DatabaseFactory.CreateDatabase();
            using (DbConnection conn = DB.CreateConnection())
            {
                //打开连接
                conn.Open();
                //创建事务
                DbTransaction trans = conn.BeginTransaction();
                try
                {
                    foreach (PartsBalanceInfo obj in list)
                    {
                        DbCommand dbCommand = DB.GetStoredProcCommand("PartsBalance_Add");
                        DB.AddInParameter(dbCommand, "DebtId", DbType.Int32, obj.DebtId);
                        DB.AddInParameter(dbCommand, "Cid", DbType.Int32, obj.Cid);
                        DB.AddInParameter(dbCommand, "BalanceMoney", DbType.Decimal, obj.BalanceMoney);
                        DB.AddInParameter(dbCommand, "CreateDate", DbType.DateTime, obj.CreateDate);
                        DB.AddInParameter(dbCommand, "RealName", DbType.String, obj.RealName);
                        DB.AddInParameter(dbCommand, "Remark", DbType.String, obj.Remark);



                        BId = Convert.ToInt32(DB.ExecuteScalar(dbCommand, trans));
                        if (BId <=0)
                        {                           
                            break;
                        }
                    }
                    if (BId > 0)
                        trans.Commit();
                    else
                        trans.Rollback();
                    return BId;        
                }
                catch
                {
                  
                    trans.Rollback();
                    return -99;
                    
                }
                finally
                {
                    //关闭连接
                    conn.Close();
                }
            }


        }

        public static IDataReader GetList(int bid)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PartsBalance_GetList", bid);
            return DB.ExecuteReader(dbCommand);
        }
        public static DataTable GetBalanceList(int CID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PartsBalance_GetBalanceList", CID);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }

        public static DataTable GetDebtBalanceList(int DebtId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PartsBalance_GetDebtBalanceList", DebtId);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }

        public static int AddAdjusting(int BID, decimal NewBalanceMoney)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("PartsBalance_AddAdjusting");
            DB.AddInParameter(dbCommand, "BID", DbType.Int32, BID);
            DB.AddInParameter(dbCommand, "NewBalanceMoney", DbType.Decimal, NewBalanceMoney);          

            try
            {
                return Convert.ToInt32( DB.ExecuteScalar(dbCommand));
                
            }
            catch (Exception exc)
            {
                return -1;
            }
        }
     }
}
